﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Odbc;
using MySql.Data.MySqlClient;

namespace DB_SQL
{
    public class ConnectionDB_SQL
    {
        public string pass;
        public string strConn;
        public OleDbConnection cnn;
        public OleDbCommand cmd;
        public MySqlConnection conn;
        #region Конструкторы
        /// <summary>
        /// Конструкторы
        /// </summary>
        public ConnectionDB_SQL() { InitializeComponentMy(); }
        public ConnectionDB_SQL(string stringPathSourse)
        { InitializeComponentMy(stringPathSourse); }
        public ConnectionDB_SQL(string stringPathSourse, bool bOpenNoOut)
        { InitializeComponentMy(stringPathSourse, bOpenNoOut); }

        public ConnectionDB_SQL(string stringPathSourse, ref string stringConnection)
        { InitializeComponentMy(stringPathSourse, ref stringConnection); }

        public ConnectionDB_SQL(ref string stringConnection)
        { InitializeComponentMySer(ref stringConnection); }

        public ConnectionDB_SQL(ref string stringConnection, string strDATABASEname)
        { InitializeComponentMySer(ref stringConnection, strDATABASEname); }
       
        #endregion

        #region функции инициализации
        /// <summary>
        /// функции инициализации
        /// </summary>
        private void InitializeComponentMy()
        {
            cnn = new OleDbConnection();conn = new MySqlConnection();
            string stringPathSourse = "C:\\work\\donet\\20074124db\\ODB\\DBparams.mdb";
            strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + stringPathSourse;
            cnn.ConnectionString = strConn;
            pass = "";
        }
        private void InitializeComponentMy(string stringPathSourse)
        {
            cnn = new OleDbConnection(); 
            strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + stringPathSourse;
            cnn.ConnectionString = strConn;
        }
        private void InitializeComponentMy(string stringPathSourse, bool bOpenNo)
        {
            cnn = new OleDbConnection();
            strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + stringPathSourse;
            cnn.ConnectionString = strConn;
            if (bOpenNo == true) { cnn.Open(); }
        }
        private void InitializeComponentMy(string stringPathSourse, ref string stringConnection)
        {
            cnn = new OleDbConnection();
            stringConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + stringPathSourse;
            cnn.ConnectionString = stringConnection;
        }
        private void ConnectionClose(){ cnn.Close();}
        private void ConnectionOpen() { cnn.Open(); }
        private void InitializeComponentMySer(ref string stringConnection)
        {
            OdbcConnection cnn = new OdbcConnection();
            stringConnection = @"DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=baselists;UID=root;PASSWORD=;OPTION=0;CHARSET=cp1251";
            cnn.ConnectionString = stringConnection;
        }
        private void InitializeComponentMySer(ref string stringConnection, string strDATABASEname)
        {
            OdbcConnection cnn = new OdbcConnection();
            stringConnection = @"DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=" + strDATABASEname + ";UID=root;PASSWORD=;OPTION=0;CHARSET=cp1251";
            cnn.ConnectionString = stringConnection;
        }
        public void setConn(string serverName)
        {
           conn = new MySqlConnection(string.Concat("server=", serverName, ";user id=muser; password=", pass  ,"; database=city; pooling=false; charset=utf8;"));
        }
        void m(object o)  { System.Windows.Forms.MessageBox.Show(o.ToString()); }
        public void setConn_mdb(string path)
        {
            cnn = new OleDbConnection();
            string stringPathSourse = path;
            strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + stringPathSourse;
            cnn.ConnectionString = strConn; 
        }
        
        public void co(string serverName)
        {
            try 
            { setConn(serverName); conn.Open(); }  
            catch  
            { System.Windows.Forms.MessageBox.Show( string.Concat("Неудалось подключиться к ", serverName) ); }
        }
        
        public void cc()     { conn.Close(); }
        public void co_mdb() { cnn.Open();   }
        public void cc_mdb() { cnn.Close();  }
        #endregion



        #region wwSQL
        


        #endregion
    }
}

